package com.dhcc.DRGsHosp.facade.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DrgsVersionManageApplication;
import com.dhcc.DRGsHosp.core.domain.Dictionary;
import com.dhcc.DRGsHosp.core.domain.DrgsVersionManage;
import com.dhcc.DRGsHosp.core.domain.DrgsVersionManageDTO;
import com.dhcc.DRGsHosp.facade.DrgsVersionManageFacade;
import com.dhcc.DRGsHosp.facade.impl.assembler.DrgsVersionManageAssembler;


@Named("drgsFacadeImplHuangju201800911")
public class DrgsVersionManageFacadeImpl implements DrgsVersionManageFacade{
	
	@Autowired
	private DrgsVersionManageApplication  application;
	
	private QueryChannelService queryChannel;
	
	
	private QueryChannelService getQueryChannelService(){
	       if(queryChannel==null){
	          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
	       }
	     return queryChannel;
	    }
		
		public InvokeResult creatDrgsVersionManage(DrgsVersionManageDTO dvmDTO) {
			DrgsVersionManage dvm = application.getDrgsVersionManage(dvmDTO.getVerCode());
			if(null == dvm){
				application.creatDrgsVersionManage(DrgsVersionManageAssembler.toEntity(dvmDTO));
				return InvokeResult.success();
			}else{
				return InvokeResult.failure("此DRGs版本编码已存在！");
			}
		}
	     
		
		
	
		public InvokeResult updateDrgsVersionManage(DrgsVersionManageDTO dvmDTO) {
			application.updateDrgsVersionManage(DrgsVersionManageAssembler.toEntity(dvmDTO));
			return InvokeResult.success();
		}
		
		public InvokeResult removeDrgsVersionManages(String[] verCodes) {
			Set<DrgsVersionManage> dvms= new HashSet<DrgsVersionManage>();
			for ( String verCode : verCodes) {
				DrgsVersionManage dvm = application.getDrgsVersionManage(verCode);
				dvms.add(dvm);
			}
			application.removeDrgsVersionManages(dvms);
			return InvokeResult.success();
		}
	
	
		public Page<DrgsVersionManageDTO> pageQueryDrgsVersionManage(DrgsVersionManageDTO queryVo, int currentPage, int pageSize) {
			List<Object> conditionVals = new ArrayList<Object>();
		   	StringBuilder jpql = new StringBuilder("select _drgsVersionManage from DrgsVersionManage _drgsVersionManage   where 1=1 ");
		   	if (queryVo.getVerCode() != null && !"".equals(queryVo.getVerCode())) {
		   		jpql.append(" and _drgsVersionManage.verCode like ?");
		   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getVerCode()));
		   	}		
		   	if (queryVo.getVerDesc() != null && !"".equals(queryVo.getVerDesc())) {
		   		jpql.append(" and _drgsVersionManage.verDesc like ?");
		   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getVerDesc()));
		   	}	
		   	if (queryVo.getRegionCode() != null && !"".equals(queryVo.getRegionCode())) {
		   		jpql.append(" and _drgsVersionManage.regionCode like ?");
		   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getRegionCode()));
		   	}	
			if (queryVo.getAddDate() != null && !"".equals(queryVo.getAddDate())) {
		   		jpql.append(" and _drgsVersionManage.addDate= ?");
		   		conditionVals.add(queryVo.getAddDate());
		   	}	
//		   	jpql.append(" order by _drgsVersionManage.verCode");
		   	
	        Page<DrgsVersionManage> pages = getQueryChannelService()
			   .createJpqlQuery(jpql.toString())
			   .setParameters(conditionVals)
			   .setPage(currentPage, pageSize)
			   .pagedList();
			   
	        //	        return null;
	        return new Page<DrgsVersionManageDTO>(pages.getStart(), pages.getResultCount(),pageSize, DrgsVersionManageAssembler.toDTOs(pages.getData()));

		}

		/**
	     *  
	     *  isverCodeExist.  
	     *  @Description：版本编码是否存在
	     *  @Author huangju 
	     *  @param verCode
	     *  @return 
	     *  @Date:2018年3月26日
	     */
	
		@Override
		public boolean isVerOrderExist(String verCode) {
			return application.isVerOrderExist(verCode)  ;
		}
	
	
	
	
	
	

}
